﻿@model Transport.Areas.Administrator.Models.VerifyCostModel
@{
    ViewBag.Title = "Duyệt Và Xác Nhận Giá";
}
@(Html.Telerik().Window()
                     .Name("tMessageBox")
                     .Title("Cảnh Báo")
                     .Draggable(true)
                     .Modal(true)
                     .Visible(false)
                     .Width(250)
                     .Height(50)
                     .Effects(fx => fx
                                   .Zoom()
                                   .Opacity()
                                   .OpenDuration(300)
                                   .CloseDuration(300)
                    )
                    .Content("</br>Số vận đơn này không tồn tại trong hệ thống!"))
@using (Html.BeginForm("VerifyCost", "Parcel", FormMethod.Post))
{
    <div class="section-header">
        <div class="title">
            <img src="@Url.Content("~/Areas/Administrator/Content/images/ico-catalog.png")" alt="" />
            Duyệt Và Xác Nhận Giá
        </div>
        <div class="options">
            <button type="submit" name="save" value="Save" tabindex="6" class="t-button">Duyệt</button>
        </div>
    </div>
    <table width="99%">
        <tr>
            <td width="100" class="adminTitle">
                @Html.Label("Số vận đơn")
                <span class="field_required">*</span>
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => model.TrackingNumbers)
                @Html.ValidationMessageFor(model => model.TrackingNumbers)
            </td>
        </tr>
    </table>
    <div id="divDetail">
        @Html.Partial("_VerifyCostDetail", Model)
    </div>
}

<script type="text/javascript">
    $(document).ready(function () {
        $('#TrackingNumbers').focus();

        $('#TrackingNumbers').keypress(function (event) {
            if (event.keyCode == 13) {
                event.preventDefault();
                $.ajax({
                    type: 'POST',
                    dataType: "json",
                    url: '@Url.Action("GetParcelForVerifyCost", "Parcel")',
                    data: { trackingNumbers: $('#TrackingNumbers').val() },
                    success: function (data) {
                        if (data == false) {
                            $('#tMessageBox').data('tWindow').center().open();
                        }
                        else {
                            $('#divDetail').html(data);
                            if ($('#chkHandInput').checked) {
                                EnableTotalCost();
                            }
                            else {
                                DisableTotalCost();
                            }
                            $('#chkHandInput').click(function () {
                                if (this.checked) {
                                    EnableTotalCost();
                                }
                                else {
                                    DisableTotalCost();
                                }

                            });
                        }
                    }
                });
            }
            else if (event.keyCode == 46) {
                $(this).val('');
            }
        });

        
    });
    function DisableTotalCost() {
        var numberTextBox = $("#TotalCost").data("tTextBox");
        numberTextBox.disable();
    }

    function EnableTotalCost() {
        var numberTextBox = $("#TotalCost").data("tTextBox");
        numberTextBox.enable();
    }

    function GetCost() {
        var deliverFromId = $("#DeliverFromId").val();
        var deliverToId = $("#DeliverToId").val();
        var totalWeight = $("#TotalWeight").data("tTextBox").value();
        var customerId = $("#CustomerId").val();
        console.log(customerId);
        if (customerId == "") {
            customerId = 0;
        }
        if (!!deliverFromId && !!deliverToId && !!totalWeight && $("#chkHandInput").is(':checked') == false) {
            $.ajax({
                url: '@Url.Action("GetCost", "Parcel")',
                type: "POST",
                dataType: "json",
                // query will be the param used by your action method
                data: { deliverFromId: deliverFromId, deliverToId: deliverToId, totalWeight: totalWeight, customerId: customerId },
                success: function (data) {
                    var totalCost = $("#TotalCost").data("tTextBox");
                    totalCost.value(data);
                    var totalCostHidden = $("#TotalCostHidden").data("tTextBox");
                    totalCostHidden.value(data);
                }
            });
        }
    }

    function ChangeTotalWeight() {
        GetCost();
    }

    function ChangeTotalCost() {
        var totalCost = $("#TotalCost").data("tTextBox");
        var totalCostHidden = $("#TotalCostHidden").data("tTextBox");
        totalCostHidden.value(totalCost.value());
    }
</script>